//
//  ScanQRViewController.m
//  LOU
//
//  Created by iMacOne on 15/11/10.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "ScanQRViewController.h"
#import "ToolUtil.h"
#import "Size.h"
#import <AVFoundation/AVFoundation.h>
@interface ScanQRViewController ()

@property (strong, nonatomic)AVCaptureDevice *device;
@property (strong, nonatomic)AVCaptureDeviceInput *input;
@property (strong, nonatomic)AVCaptureMetadataOutput *output;
@property (strong, nonatomic)AVCaptureSession *session;
@property (strong, nonatomic)AVCaptureVideoPreviewLayer *preview;

@end

@implementation ScanQRViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [self.timer invalidate];
    self.timer = nil;
    [super viewWillDisappear:animated];
}
- (void)viewDidLoad {
    
    [self setupCamera];
    
    float y = [self createTitle:@"" midTitle:@"扫描二维码"];
    float scannerSize = WindowWidth * 0.66, scanner_x = (WindowWidth - scannerSize) / 2,scanner_y = scanner_x;
    self.scannerView = [[UIView alloc] initWithFrame:CGRectMake(scanner_x, scanner_y, scannerSize, scannerSize)];
    UIView * coverView = [[UIView alloc] initWithFrame:CGRectMake(0, y, WindowWidth, WindowHeight - y)];
    coverView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:coverView];
    [self createBorder:coverView];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerThread) userInfo:nil repeats:YES];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)setupCamera
{
    self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    self.output = [[AVCaptureMetadataOutput alloc] init];
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    self.session = [[AVCaptureSession alloc] init];
    [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    if ([self.session canAddInput:self.input]) {
        [self.session addInput:self.input];
    }
    if ([self.session canAddOutput:self.output]) {
        [self.session addOutput:self.output];
    }
    //条形码类型
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    
    self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.preview.frame = CGRectMake(0, margin_64, WindowWidth, WindowHeight);
    [self.view.layer insertSublayer:self.preview atIndex:0];
    
    //开始
    [self.session startRunning];
}
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    NSString * stringValue;
    if (metadataObjects.count > 0) {
        AVMetadataMachineReadableCodeObject * metadataObjict = [metadataObjects objectAtIndex:0];
        stringValue = metadataObjict.stringValue;
    }
    [self dismissViewControllerAnimated:NO completion:^{
        [_session stopRunning];
        [self.delegate scanResult:stringValue];
    }];
}
//创建四个直角和中线
- (void)createBorder:(UIView *)view
{
    float scanner_x = self.scannerView.frame.origin.x, scanner_y = self.scannerView.frame.origin.y;
    
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scanner_x, view.frame.size.height)];
    leftView.backgroundColor = [UIColor blackColor];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.scannerView.frame), 0, view.frame.size.width - CGRectGetMaxX(self.scannerView.frame), view.frame.size.width)];
    rightView.backgroundColor = [UIColor blackColor];
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(self.scannerView.frame.origin.x, 0, self.scannerView.frame.size.width, self.scannerView.frame.origin.y)];
    topView.backgroundColor = [UIColor blackColor];
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(self.scannerView.frame.origin.x, CGRectGetMaxY(self.scannerView.frame), self.scannerView.frame.size.width, view.frame.size.height-CGRectGetMaxY(self.scannerView.frame))];
    bottomView.backgroundColor = [UIColor blackColor];
    
    [view addSubview:leftView];
    [view addSubview:rightView];
    [view addSubview:topView];
    [view addSubview:bottomView];
    
    
    int line_thick = margin_3, line_long = line_thick * 8, middleLine_w = _scannerView.frame.size.width - line_thick * 2;
    UIView *top_left = [[UIView alloc] init];
    top_left.frame = CGRectMake(scanner_x - line_thick, scanner_y - line_thick, line_long, line_thick);
    top_left.backgroundColor =[UIColor blackColor];
    [view addSubview:top_left];
    UIView *left_top = [[UIView alloc] init];
    left_top.frame = CGRectMake(scanner_x - line_thick, scanner_y - line_thick, line_thick, line_long);
    left_top.backgroundColor = [UIColor blackColor];
    [view addSubview:left_top];
    
    UIView *top_right = [[UIView alloc] init];
    top_right.frame = CGRectMake(CGRectGetMaxX(_scannerView.frame) - (line_long - line_thick), scanner_y - line_thick, line_long, line_thick);
    top_right.backgroundColor = [UIColor blackColor];
    [view addSubview:top_right];
    UIView *right_top = [[UIView alloc] init];
    right_top.frame = CGRectMake(CGRectGetMaxX(_scannerView.frame), scanner_y - line_thick, line_thick, line_long);
    right_top.backgroundColor = [UIColor blackColor];
    [view addSubview:right_top];
    
    UIView *bottom_left = [[UIView alloc] init];
    bottom_left.frame = CGRectMake(scanner_x - line_thick, CGRectGetMaxY(_scannerView.frame), line_long, line_thick);
    bottom_left.backgroundColor = [UIColor blackColor];
    [view addSubview:bottom_left];
    UIView *left_bottom = [[UIView alloc] init];
    left_bottom.frame = CGRectMake(scanner_x - line_thick, CGRectGetMaxY(_scannerView.frame) - (line_long - line_thick), line_thick, line_long);
    left_bottom.backgroundColor = [UIColor blackColor];
    [view addSubview:left_bottom];
    
    UIView *bottom_right = [[UIView alloc] init];
    bottom_right.frame = CGRectMake(CGRectGetMaxX(_scannerView.frame) - (line_long - line_thick), CGRectGetMaxY(_scannerView.frame), line_long, line_thick);
    bottom_right.backgroundColor = [UIColor blackColor];
    [view addSubview:bottom_right];
    UIView *right_bottom = [[UIView alloc] init];
    right_bottom.frame = CGRectMake(CGRectGetMaxX(_scannerView.frame), CGRectGetMaxY(_scannerView.frame) - (line_long - line_thick), line_thick, line_long);
    right_bottom.backgroundColor = [UIColor blackColor];
    [view addSubview:right_bottom];
    
    
    self.middleLine = [[UIImageView alloc] init];
    _middleLine.image = [UIImage imageNamed:@""];
    _middleLine.frame = CGRectMake(scanner_x + line_thick, scanner_y + line_thick, middleLine_w, 10.);
    [view addSubview:_middleLine];
    
    UILabel *lblText = [[UILabel alloc] init];
    lblText.backgroundColor = [UIColor clearColor];
    NSString *text = @"将二维码放入框内，即可自动扫描";
    if (![ToolUtil isBlank:_tipsText]) {
        text = _tipsText;
    }
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName : FONT(14)}];
    lblText.frame = CGRectMake((WindowWidth - size.width) / 2, CGRectGetMaxY(_scannerView.frame) + line_long * 2, size.width, size.height);
    lblText.text = text;
    [lblText setFont:FONT(14)];
    lblText.textColor = [UIColor whiteColor];
    [view addSubview:lblText];
    
}
//中线走的线程
- (void)timerThread
{
    CGRect tempFrame = _middleLine.frame;
    if (tempFrame.origin.y >= _scannerView.frame.origin.y + tempFrame.size.height && tempFrame.origin.y < CGRectGetMaxY(_scannerView.frame) - tempFrame.size.height * 2) {
        tempFrame.origin.y += 1;
    }else{
        tempFrame.origin.y = _scannerView.frame.origin.y + tempFrame.size.height;
    }
    _middleLine.frame = tempFrame;
}
//关闭界面
- (void)doBack:(UIButton *)btnBack
{
    [self.timer invalidate];
    self.timer = nil;
    [super doBack:btnBack];
    
}
@end
